package com.brayan.webapp.repository.general.i18n;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import org.apache.log4j.Logger;

import com.brayan.webapp.model.general.i18n.Language;
import com.brayan.webapp.repository.realestate.ReservationRepository;

@Stateless
public class LanguageRepository {
	
	private static Logger LOGGER = Logger.getLogger(ReservationRepository.class);
	
	@PersistenceContext(unitName = "ccms")
	EntityManager em;
	
	public Language getLanguage(String languageCode){
		return em.find(Language.class, languageCode);
	}
	

	public List<Language> getLanguages() {
		
		CriteriaBuilder cb = em.getCriteriaBuilder();
		// Form the query.
		CriteriaQuery<Language> critQuery = cb.createQuery(Language.class);
		Root<Language> root = critQuery.from(Language.class);
		critQuery.select(root);
		
		// Execute the query.
		TypedQuery<Language> query = em.createQuery(critQuery);
		List<Language> languages = query.getResultList();
		return languages;
	}


	public void save(Language language) {
		em.merge(language);		
	}
}
